package com.example.minemodule.ui;

import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.examination.mlib.constants.ARouterConstants;
import com.examination.mlib.interfaceapi.MineApi;
import com.example.minemodule.R;
import com.example.minemodule.Utils.MineUtils;
import com.example.minemodule.databinding.RecordActivityDataBinding;
import com.example.minemodule.viewmodel.RecordViewModel;
import com.yilijk.base.base.BaseActivity;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.ToastUtils;
import com.zkk.view.rulerview.RulerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

@Route(path = ARouterConstants.RecordWeightActivity)
public class RecordWeightActivity extends BaseActivity<RecordViewModel, RecordActivityDataBinding> {


    @Autowired
    public int pageType;
    @Autowired
    public int inquiringPatientId = 0;
    @Autowired
    public int height = 160;
    @Autowired
    public float weight = 55.0f;
    @Autowired
    public float blood = 4.0f;
    @Autowired
    public String times;
    @Autowired
    public int id = 0;
    @Autowired
    public int bloodFatRecordSource = 1;  //血脂数据来源 1：手动上传 2：随访问卷

    @Override
    protected int getContentViewId() {
        return R.layout.activity_recordweight;
    }


    @Override
    protected void processLogic() {
        setUseStatusBarColor(R.color.color_F4F4F4, false, true);
        mViewModel.setBinding(this, binding);
        mViewModel.initUI(pageType,inquiringPatientId,id);
        switch (pageType){
            case 0:
                mViewModel.initHeight(height, weight);
                break;
            case 1:
                mViewModel.initBlood(blood,times);
                break;
        }

    }

    @Override
    protected void dispostData() {

    }

    @Override
    protected void setListener() {
        ClickUtils.setFastOnClickListener(binding.saveWeight, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (pageType){
                    case 0:
                        mViewModel.saveHeight();
                        break;
                    case 1:
                        if (binding.getBloodTime()==null || TextUtils.isEmpty(binding.getBloodTime())){
                            ToastUtils.showShort("选择测量时间");
                        }else{
                            mViewModel.saveBlood(inquiringPatientId,binding.getBloodValue(),binding.getBloodTime(),bloodFatRecordSource+"");
                        }
                        break;
                }
            }
        });
        switch (pageType) {
            case 0:
                binding.rulerHeight.setOnValueChangeListener(new RulerView.OnValueChangeListener() {
                    @Override
                    public void onValueChange(float value) {
                        binding.setHeightValue((int) value + "");
                        height = (int) value;
//                tv_register_info_weight_value.setText(value+"");
                    }
                });

                binding.rulerWeight.setOnValueChangeListener(new RulerView.OnValueChangeListener() {
                    @Override
                    public void onValueChange(float value) {
                        binding.setWeightValue(value + "");
                        weight = (float) value;
//                tv_register_info_weight_value.setText(value+"");
                    }
                });
                break;
            case 1:
                binding.rulerBlood.setOnValueChangeListener(new RulerView.OnValueChangeListener() {
                    @Override
                    public void onValueChange(float value) {
                        binding.setBloodValue(value + "");
                        blood = (float) value;
                    }
                });
                ClickUtils.setFastOnClickListener(binding.selectTimeCv, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HashMap<String, Object> map = new HashMap<>();
                        HashMap<String, Object> datamap = new HashMap<>();
                        datamap.put("data",new ArrayList<String>());
                        map.put("data",datamap);
                        map.put("title","选择测量时间");
                        map.put("type","time_HM");
                        map.put("mark","");
                        MineUtils.openPickView(map, RecordWeightActivity.this, new MineApi() {
                            @Override
                            public void onSuccess(String msg) {
//{"selInfo":{"title":"2021-11-12 20:18","mark":"2021-11-12 20:18"},"title":"选择测量时间"}
                                try {
                                    JSONObject jsonObject = new JSONObject(msg);
                                    JSONObject selInfo = jsonObject.getJSONObject("selInfo");
                                    String title = selInfo.getString("title");
                                    binding.setBloodTime(title);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }

                            @Override
                            public void onError(String failInfo) {

                            }
                        });
                    }
                });
                break;
        }

    }

    @Override
    public void onClick(View v) {

    }

}
